<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use app\admin\model\Fir;
class Index extends Controller
{
    protected function _initialize()
    {
    	if(empty(session('username'))){
    		$this->redirect('Login/index');
    	}
    }	
    public function index()
    {
		return $this->fetch();
    }
	public function outlogin()
	{
	    session(null);
	    $this->redirect('Login/index');
	}
	public function frlist()
	{
		$list = Fir::order('create_time','desc')->select();
		$this->assign('list',$list);
		$this->assign('count',count($list));
		return $this->fetch();
	}
	public function addf()
	{
        	return $this->fetch();		
	}
	public function addfriend(Request $request)
	{
		#转换接收参数
		$name=$request->param('name');
		$sex=$request->param('sex');
		$birthtype=$request->param('birthtype');
		$birthday=$request->param('birthday');
		$phone1=$request->param('phone1');
		$phone2=$request->param('phone2');
		$QQ=$request->param('QQ');
		$wechat=$request->param('wechat');
		$mail=$request->param('mail');
		$lovely=$request->param('lovely');
		$hate=$request->param('hate');
		$ourstory=$request->param('ourstory');
		$other=$request->param('other');
		#写入数据操作
		$user = new Fir;
		$user->name=$name;
		$user->sex=$sex;
		$user->birthtype=$birthtype;
		$user->birthday=$birthday;
		$user->phone1=$phone1;
		$user->phone2=$phone2;
		$user->QQ=$QQ;
		$user->wechat=$wechat;
		$user->mail=$mail;
		$user->lovely=$lovely;
		$user->hate=$hate;
		$user->ourstory=$ourstory;
		$user->other=$other;
		if($user->save())
		{
			$this->redirect('Index/frlist');
		}
		else
		{
			return 'fail';
		}
	}
	/*
	将今天转换为农历的日期(生日)，并取出来与数据库的农历日期对比
	*/
	public function nls()
	{
		$datatime=date('Ymd'); #定义当前的日期为2017年10月
		$nl=nongli($datatime); #通过nongli函数把当前日期转换为农历的日期
		$nlmonth=$nl['month']; #获取到农历的月份
		$nlday=$nl['day']; #获取到农历的日
		$list = Fir::all(['birthtype'=>'农历']);#查询数据库农历生日的人
		$userlist=array();
		foreach ($list as $user)
		{ #遍历数据库
			$birthday=$user->birthday; #获取birthday的时间
			$d=strtotime($birthday); #将生日时间转换为时间戳
			$srm=date('m',$d); #取出数据库农历生日的月份
			if($nlmonth==$srm)
			{ #判断当前的生日月份与数据库生日的月份是否相符，如果相符就打印输出
				$srday=date('d',$d);
				$srzt=$srday-$nlday;
				array_push($userlist,array('name'=>$user->name,'birthtype'=>$user->birthtype,'birthday'=>$user->birthday,'sex'=>$user->sex,'phone1'=>$user->phone1,'create_time'=>$user->create_time,'srzt'=>$srzt));
			}
		}
		$this->assign('userlist',$userlist);
		return $this->fetch();
	}
	public function sr()
	{
		$datatime=date('Ymd'); #定义当前的日期为2017年10月
		$nl=nongli($datatime); #通过nongli函数把当前日期转换为农历的日期
		$nlmonth=$nl['month']; #获取到农历的月份
		$nlday=$nl['day']; #获取到农历的日
		$datem=date('m');
		#$list = Fir::all();#查询数据库农历生日的人
		#$list = Fir::order('birthday','desc')->select();
		$list = Fir::where("FROM_UNIXTIME(birthday,'%m')",$datem)->order("FROM_UNIXTIME(birthday,'%m%d')",'desc')->select();
		$userlist=array();
		foreach ($list as $user) 
		{
			if($user->birthtype=='农历')
			{
				$birthday=$user->birthday; #获取birthday的时间
				$d=strtotime($birthday); #将生日时间转换为时间戳
				$srm=date('m',$d); #取出数据库农历生日的月份
				if($nlmonth==$srm)
				{ #判断当前的生日月份与数据库生日的月份是否相符，如果相符就打印输出
					$srday=date('d',$d);
					$srzt=$srday-$nlday;
					array_push($userlist,array('name'=>$user->name,'birthtype'=>$user->birthtype,'birthday'=>$user->birthday,'sex'=>$user->sex,'phone1'=>$user->phone1,'create_time'=>$user->create_time,'srzt'=>$srzt));
				}
			}
			else
			{
				$birthday=$user->birthday; #获取birthday的时间
				$d=strtotime($birthday); #将生日时间转换为时间戳
				$srm=date('m',$d); #取出数据库新历生日的月份
				$xlm=date('m');
				if($xlm==$srm)
				{ #判断当前的生日月份与数据库生日的月份是否相符，如果相符就打印输出
					$srday=date('d',$d);
					$xlday=date('d');
					$srzt=$srday-$xlday; #生日天数
					array_push($userlist,array('name'=>$user->name,'birthtype'=>$user->birthtype,'birthday'=>$user->birthday,'sex'=>$user->sex,'phone1'=>$user->phone1,'create_time'=>$user->create_time,'srzt'=>$srzt));
				}				
			}				
		}
		$this->assign('userlist',$userlist);
		return $this->fetch();
	}
	/*
	public function sr()
	{
		$datatime=date('Ymd'); #定义当前的日期为2017年10月
		$nl=nongli($datatime); #通过nongli函数把当前日期转换为农历的日期
		$nlmonth=$nl['month']; #获取到农历的月份
		$nlday=$nl['day']; #获取到农历的日
		$datem=date('m');
		#$list = Fir::all();#查询数据库农历生日的人
		#$list = Fir::order('birthday','desc')->select();
		$e="FROM_UNIXTIME(birthday,'%m')";
		$list = Fir::where("FROM_UNIXTIME(birthday,'%m')",'==','08')->order("FROM_UNIXTIME(birthday,'%m%d')",'desc')->select();
		$userlist=array();
		foreach ($list as $user) 
		{
			$birthday=$user->birthday; #获取birthday的时间
			$d=strtotime($birthday); #将生日时间转换为时间戳
			if($user->birthtype=='农历')
			{
				$srday=date('d',$d);
				$srzt=$srday-$nlday;
				array_push($userlist,array('name'=>$user->name,'birthtype'=>$user->birthtype,'birthday'=>$user->birthday,'sex'=>$user->sex,'phone1'=>$user->phone1,'create_time'=>$user->create_time,'srzt'=>$srzt));
				
			}
			else
			{
				$srday=date('d',$d);
				$xlday=date('d');
				$srzt=$srday-$xlday; #生日天数
				array_push($userlist,array('name'=>$user->name,'birthtype'=>$user->birthtype,'birthday'=>$user->birthday,'sex'=>$user->sex,'phone1'=>$user->phone1,'create_time'=>$user->create_time,'srzt'=>$srzt));			
			}				
		}
		$this->assign('userlist',$userlist);
		return $this->fetch();
	}
	public function fi()	
	{
		$datem=date('m');
		$list = Fir::where("FROM_UNIXTIME(birthday,'%m')","08")->select();
		foreach ($list as  $user) {
			echo $user->name;
			echo $user->birthday;
		}
	}*/
}